package gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JComponent;

import util.Vector;

import environment.Ball;
import environment.Pitch;
import environment.player.Player;

public class PitchComponent extends JComponent
{	
	private final Pitch pitch;
	private int selectedPlayerID;
	private boolean ballSelected;
	
	public PitchComponent(final Pitch pitch)
	{
		this.pitch = pitch;
		
		selectedPlayerID = -1;
//		ballSelected = false;
		
		addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				Vector click = new Vector(e.getX(), e.getY());
				
				if (selectedPlayerID != -1)
				{
					for (Player p : pitch.getPlayers())
					{
						if (p.getID() == selectedPlayerID)
						{
							p.setPosition(click);
							selectedPlayerID = -1;
						}
					}
				}
				else
				{
					for (Player p : pitch.getPlayers())
					{
						if (click.euclDistance(p.getPosition()) <= 10)
						{
							selectedPlayerID = p.getID();
						}
					}
					
					if (selectedPlayerID == -1) pitch.getBall().setPosition(click);
					
//					if (click.euclDistance(pitch.getBall().getPosition()) <= 5)
//					{
//						ballSelected = true;
//					}
				}
			}
		});
	}
	
	public void paintComponent(Graphics g)
	{
		// collect width & height
		double width = pitch.getWidth();
		double height = pitch.getHeight();
		
		
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		// draw score
		g2.setFont(new Font("Dialog", Font.PLAIN, 20));
		g2.drawString(pitch.getScore()[0] + ":" + pitch.getScore()[1], 20, 30);
		
		// draw field outlines
		Line2D.Double left = new Line2D.Double(0, 0, 0, height);
		Line2D.Double top = new Line2D.Double(0, 0, width, 0);
		Line2D.Double right = new Line2D.Double(width, 0, width, height);
		Line2D.Double bottom = new Line2D.Double(0, height, width, height);
		g2.setStroke(new BasicStroke(3));
		g2.draw(left);
		g2.draw(top);
		g2.draw(right);
		g2.draw(bottom);
		g2.setStroke(new BasicStroke(1));
		
		// draw goal
		Line2D.Double goalAwayTop = new Line2D.Double(-5, height/2 - 70, 5, height/2 - 70);
		Line2D.Double goalAwayBottom = new Line2D.Double(-5, height/2 + 70, 5, height/2 + 70);
		Line2D.Double goalHomeTop = new Line2D.Double(width-5, height/2 -70, width+5, height/2 - 70);
		Line2D.Double goalHomeBottom = new Line2D.Double(width-5, height/2 + 70, width+5, height/2 + 70);
		g2.setStroke(new BasicStroke(3));
		g2.draw(goalAwayTop);
		g2.draw(goalAwayBottom);
		g2.draw(goalHomeTop);
		g2.draw(goalHomeBottom);
		g2.setStroke(new BasicStroke(1));
		
		// draw boxes
		Line2D.Double boxAwayTop = new Line2D.Double(0, 90, 60, 90);
		Line2D.Double boxAwayCenter = new Line2D.Double(60, 90, 60, 90 + 220);
		Line2D.Double boxAwayBottom = new Line2D.Double(0, 90 + 220, 60, 90 + 220);
		Line2D.Double boxHomeTop = new Line2D.Double(width-60, 90, width, 90);
		Line2D.Double boxHomeCenter = new Line2D.Double(width-60, 90, width-60, 90 + 220);
		Line2D.Double boxHomeBottom = new Line2D.Double(width-60, 90 + 220, width, 90 + 220);
		g2.setStroke(new BasicStroke(3));
		g2.draw(boxAwayTop);
		g2.draw(boxAwayCenter);
		g2.draw(boxAwayBottom);
		g2.draw(boxHomeTop);
		g2.draw(boxHomeCenter);
		g2.draw(boxHomeBottom);
		g2.setStroke(new BasicStroke(1));
		
		// draw middle line and middle circle
		Line2D.Double middleLine = new Line2D.Double(width/2, 0, width/2, height);
		Ellipse2D.Double middleCircle = new Ellipse2D.Double(width/2 - 60, height/2 - 60, 120, 120);
		g2.setStroke(new BasicStroke(3));
		g2.draw(middleLine);
		g2.draw(middleCircle);
		g2.setStroke(new BasicStroke(1));
		
		// draw pitchZones
		for (int i = 0; i <= 6; i++)
		{
			Line2D.Double line = new Line2D.Double(i * width / 6, 0, i * width / 6, height);
			g2.draw(line);
		}
		for (int i = 0; i <= 4; i++)
		{
			Line2D.Double line = new Line2D.Double(0, i * height / 4, width, i * height / 4);
			g2.draw(line);
		}
		
		
		ArrayList<Player> players = pitch.getPlayers();
		
		for (Player player : players)
		{
			player.draw(g2);
		}
		
		Ball ball = pitch.getBall();
		ball.draw(g2);
	}
}
